//Monitored Events
! m1 <c>[end (call (* c.m1 ()))]:{
  
 input = 1;
}

! m2 <c>[end (call (* c.m2 ()))]:{
  
 input = 2;
}

//State Declaration
  @INTSTACK $stack = new @INTSTACK();
  @INTSTACK $end_stack = new @INTSTACK();
  int $done = 0;
  int input;
  


static int [ ] [ ] [ ] at = { { { 0 , -2 } , { 0 , 1 } , { 0 , 5 } } , { { 0 ,
    -2 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } , { { 0 , -2 } , { 1 , 1 , 2 } , { 1
    , 1 , 2 } } , { { 0 , -2 } , { 0 , 2 } , { 0 , 6 } } , { { 2 } , { 0 , -2 }
    , { 0 , -2 } } , { { 1 , 0 , 1 } , { 0 , -2 } , { 0 , -2 } } , { { 1 , 0 ,
    2 } , { 0 , -2 } , { 0 , -2 } } } ;
 
static int [ ] [ ] gt = { { 4 , 3 } , { -2 , -2 } , { -2 , -2 } , { -2 , -2 } ,
    { -2 , -2 } , { -2 , -2 } , { -2 , -2 } } ;
 


//Initialization
  $stack.push(0);

//Reset
  $stack.clear();
  $stack.push(0);

//Monitoring Body
int state;
int next_state;
int old_state;
int non_terminal;
done = 0;

END:
while(true){
  /* invalid input */
  if(input == -1){
    $done = -2;
    break END;
  }
  state = $stack.peek();
  /* state can be -2 if gt[previous_state][non_terminal] == -2 */
  /* on previous iteration of outer loop                       */
  if(state == -2){
	$done = -2;
    break END;
  }
  /* shift */
  else if(at[state][input][0] == 0){
	next_state = at[state][input][1];
    /* invalid input at this state, return violation */
    if(next_state == -2){
	  $done = -2;
      break END;
	}
    /* shift to a valid state */
    else{
      $stack.push(next_state);
      /* check for an unbounded number of reductions of an accept */
      /* with 0 (end of input) as the lookahead                   */
      if(at[next_state][0][0] == 1){
		 $end_stack = $stack.fclone();
		 while(true){
			state = $end_stack.peek();
			/* state can be -2 if gt[previous_state][non_terminal] == -2 */
			/* on previous iteration of inner loop                       */
			if(state == -2){
				break END;
			}
			/* shift(-2) for 0, this is default, simply break */
			else if(at[state][0][0] == 0){
				break END;
			}
			/* reduction with 0 as lookahead */
			else if(at[state][0][0] == 1){
				non_terminal = at[state][0][1];
				$end_stack.pop(at[state][0][2]);
				old_state = $end_stack.peek();
				$end_stack.push(gt[old_state][non_terminal]);
			}
			/* accept with 0 as lookahead */
			else if(at[state][0][0] == 2){
				$done = -1;
				break END;
			}
			/* algorithm error? */
			else{
				$done = -3;
				break END;
			}
		 }
	  }
	  else{
		  break END;
	  }
	}
  }
  /* reduce                                                                    */
  /* note that we don't check for 0 as an input (i.e. index 0 for input)       */
  /* because any reduction chain with non-0 lookahead must end with            */
  /* a shift or an error, i.e. we can never stop with a reduce, otherwise      */
  /* we would lose our event.  All events must be shifted or cause a violation */
  else if(at[state][input][0] == 1){
	  non_terminal = at[state][input][1];
	  $stack.pop(at[state][input][2]);
	  old_state = $stack.peek();
	  $stack.push(gt[old_state][non_terminal]);
  }
  /* algorithm error? */
  else{
	  $done = -3;
	  break END;
  }
}

//Success Condition
  $done == -1
 
//Failure Condition
  $done == -2

